var app=sm("do_App");
var page=sm("do_Page");
var nf = sm("do_Notification");
var global=sm("do_Global");
var cache = sm("do_DataCache");
var device=sm("do_Device");
var config=require("config");

var do_my=ui("do_my");
var do_approve=ui("do_approve");
var do_balance=ui("do_balance");
var do_stream=ui("do_stream");
var do_build=ui("do_build");
var do_home=ui("do_home");

var userinfo=cache.loadData("userinfo");

var canBack = false;
var delay3 = mm("do_Timer");
delay3.delay = 3000;
delay3.interval = 1000;
delay3.on("tick", function(){
    this.stop();
    canBack = false;
});
page.on("back", function(){
    if (canBack) {
        global.exit();
    } else {
        nf.toast("再按一次退出");
        canBack = true;
        delay3.start();
    }
});

var http = mm("do_Http");
http.method = "POST";  
http.timeout = 30000; 
http.contentType = "application/json"; 
http.url = config.baseUrl+"getApproveList"; 
http.body = JSON.stringify({userId:userinfo.userid,approveType:0,pageIndex:1,pageSize:20,usernum:userinfo.usernum,userId:userinfo.userid});
http.on("success", function(data) {
	if(data.result==1 && data.data){
		if(data.data.length>0){
			ui("do_num").visible=true;
			ui("do_num").text=data.data.length;
		}
		else{	
			ui("do_num").visible=false;
			app.fire("setIcon");
		}
	}	
});
http.on("fail", function(data) {
	nf.toast(data.msg || "网络超时");
});
page.on("loaded",function(){
	http.request();
	updateVer();	
	if(userinfo.isupdatepwd) nf.toast("请定时修改密码");
});
app.on("updateIndex",function(){
	http.request();
});
do_my.on("touch",function(){	
	app.openPage({
		source:"source://view/my.ui",
		animationType:'slide_r2l'
	});
});
do_approve.on("touch",function(){
	app.openPage({
		source:"source://view/approve/approveList.ui",
		animationType:'slide_r2l'
		})
}).on("touchDown", function(){
	do_approve.bgColor = "E0E0E0FF";
}).on("touchUp", function(){
	do_approve.bgColor = "FFFFFFFF";
});
do_balance.on("touch",function(){	
	app.openPage({
//		source:"source://view/money/balanceSeach.ui",
		source:"source://view/money/classify.ui",
		animationType:'slide_r2l'
		});
}).on("touchDown", function(){
	do_balance.bgColor = "E0E0E0FF";
}).on("touchUp", function(){
	do_balance.bgColor = "FFFFFFFF";
});
do_stream.on("touch",function(){
	app.openPage({
		source:"source://view/stream/companyList.ui",
		animationType:'slide_r2l'
		
	});
}).on("touchDown", function(){
	do_stream.bgColor = "E0E0E0FF";
}).on("touchUp", function(){
	do_stream.bgColor = "FFFFFFFF";
});
do_home.on("touchDown", function(){
	do_home.bgColor = "E0E0E0FF";
}).on("touchUp", function(){
	do_home.bgColor = "FFFFFFFF";
});
page.on("result",function(result){
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:1});
});

var do_timer=mm("do_Timer");
do_timer.interval = 15*1000;
do_timer.start();

do_timer.on("tick",function(){
	http.request();
});

function updateVer(){
	var getVerSion = global.getVersion().ver;
	var uhttp = mm("do_Http");
	uhttp.method="POST";
	uhttp.timeout = 30000;
	uhttp.contentType = "application/json";
	uhttp.url = config.baseUrl+"getAppUpdateInfo";
	uhttp.body = JSON.stringify({appId:app.getAppID(),platform:1,mobileVersion:getVerSion});
	uhttp.on("success", function(data, e) {
		if(data.result==1 && data.data){
			if (data.data.isLatestVersion == "true") {  //是否对当前版本更新
				nf.confirm("有新版本，是否进行版本更新？","版本更新提示", "更新", "取消", function(index) {
					if(index=="1")
						{
						var appURL = (device.getInfo().OS=="android")?data.data.androidurl:data.data.iosurl;
						sm("do_External").openURL(appURL);
						}
				})
			}
		}
		
	});
	uhttp.on("fail",function(data, e) {
		deviceone.print("错误"+JSON.stringify(data));
	});
	uhttp.request();
}